package com.yunpuvip.sachet.rest.controller.port;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunpuvip.sachet.biz.entity.BalanceHistory;
import com.yunpuvip.sachet.biz.entity.Player;
import com.yunpuvip.sachet.biz.service.BalanceHistoryService;
import com.yunpuvip.sachet.biz.service.OrderService;
import com.yunpuvip.sachet.biz.service.PaymentService;
import com.yunpuvip.sachet.modular.constant.enumconstant.OrderStatus;
import com.yunpuvip.sachet.modular.constant.response.ServerResponse;
import com.yunpuvip.sachet.modular.core.exception.RestServiceExceptionEnum;
import com.yunpuvip.sachet.modular.util.cache.CacheKit;
import com.yunpuvip.sachet.modular.vo.rest.request.BalanceReqVo;
import com.yunpuvip.sachet.modular.vo.rest.request.OrderQueryVo;
import com.yunpuvip.sachet.modular.vo.rest.request.PaymentReqVo;
import com.yunpuvip.sachet.modular.vo.rest.response.OrderVo;
import com.yunpuvip.sachet.modular.vo.rest.response.PaymentRspVo;
import com.yunpuvip.sachet.rest.controller.common.RestBaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ auther wushaohong
 * @ date  2019/8/10
 **/
@RequestMapping("order")
@RestController
@Slf4j
public class OrderControler  extends RestBaseController {


    @Autowired
    OrderService orderService;

    @Autowired
    PaymentService paymentService;

    @Autowired
    BalanceHistoryService balanceHistoryService;



    @RequestMapping("orderList")
    public ServerResponse getOrderList(OrderQueryVo orderQueryVo) {
        if(ObjectUtil.isNull(orderQueryVo)){
             orderQueryVo = new OrderQueryVo();
        }
        Integer id = getPlayer().getId();
        orderQueryVo.setPlayerId(id);
        Page<OrderVo> voPage = orderService.getOrderByCondition(orderQueryVo);
        log.info("获取订单列表:{}",voPage);
        return ServerResponse.creatBySuccess(voPage);
    }

    @RequestMapping("getOrder")
    public ServerResponse getOrder(@RequestParam Integer  orderId) {
        OrderVo order = orderService.getOrderById(orderId);
        if(ObjectUtil.isNull(order)||ObjectUtil.isNull(order.getPaymentId())){
            return ServerResponse.creatByErrorEnum(RestServiceExceptionEnum.ORDER_NOT_EXIST);
        }
        log.info("获取订单列表:{}",order);
        return ServerResponse.creatBySuccess(order);
    }


    @RequestMapping("balanceList")
    public ServerResponse balanceList(BalanceReqVo balanceReqVo) {
        if(ObjectUtil.isNull(balanceReqVo)){
            balanceReqVo = new BalanceReqVo();
        }
        Integer id = getPlayer().getId();
        balanceReqVo.setPlayerId(id);
        IPage<BalanceHistory> balancePage = balanceHistoryService.getBalancePage(balanceReqVo);
        log.info("获取余额列表:{}",balancePage);
        return ServerResponse.creatBySuccess(balancePage);
    }


    @RequestMapping("paymentList")
    public ServerResponse paymentList(PaymentReqVo paymentReqVo) {
        if(ObjectUtil.isNull(paymentReqVo)){
            paymentReqVo = new PaymentReqVo();
        }
        Integer id = getPlayer().getId();
        paymentReqVo.setPlayerId(id);
        Page<PaymentRspVo> paymentPage = paymentService.getPaymentPage(paymentReqVo);
        log.info("获取支付记录列表:{}",paymentPage);
        return ServerResponse.creatBySuccess(paymentPage);
    }
}
